﻿@page "{departmentId:int}"
@using Microsoft.AspNetCore.Mvc.RazorPages
@using DotNetCoreStudy.Page.Models
@using DotNetCoreStudy.Page.services

@model EmployeeListModel

<div class="row"> 
    <div class="col-md-10 offset-md-2">
        <table class="table">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Gender</th>
                <th>Is Fired</th>
                <th>操作</th>
            </tr>
            @Html.DisplayFor(expression:x=>x.Employees)
        </table>
        <a asp-page="AddEmployee" asp-route-departmentId="@ViewData["DepartmentId"]">Add</a>
    </div>
</div>

@functions {
    public class EmployeeListModel : PageModel
    {
        private readonly IEmployeeService _employeeService;

        public EmployeeListModel(services.IEmployeeService employeeService)
        {
            _employeeService = employeeService;
        }

        public IEnumerable<Models.Employee> Employees { get; set; }

        public async Task OnGet(int departmentId)
        {
            Employees = await _employeeService.GetEmployeeByDepartmentId(departmentId);
            ViewData["DepartmentId"] = departmentId;
        }

        public async Task<IActionResult> OnGetFireAsync(int employeeId,int departmentId)
        {
            await _employeeService.Fire(employeeId);
            return RedirectToPage("EmployeeList", routeValues: new { departmentId });
        }
    }
}